﻿using System.Text.Json.Nodes;
using System.Text;
using HttpMultipartParser;
using System.IO;
using System.Threading.Channels;

namespace vocsclient
{
    internal class Program
    {
        static readonly HttpClient client = new HttpClient();
        static readonly string inputString = "{\"data\": \"The result will be millions of Americans who have insurance who did not have it before.Thats the goal. And the goal is not about the website.\", \"content_type\": \"text/plain;charset=iso8859-1\", \"content_encoding\": \"8bit\"}";
        static readonly string paramString = "{\"language\": \"en-us\", \"voice\": \"zoe-ml\", \"volume\": \"80\", \"audio_chunk_duration_ms\": \"20000\", \"sample_rate\": \"16000\", \"output_type\": \"10\", \"rate\": \"50\", \"session_id\": \"e73db992-5301-4a85-b333-2b4e77e9db81\",\"log_event\": \"1\",\"lipsync_info\": \"1\"}";
        //static readonly string url = "https://leiyang.icu/post";
        //static readonly string url = "http://47.108.196.76:8080/post";
        //static readonly string url = "https://tts-qa.na.onecloud.hosting.cerence.net/vocs/speak";
        static readonly string url = "http://10.192.128.28:28166/vocs/speak";
        static async Task Main(string[] args)
        {
            try
            {
                string boundary = "NudUD_5paZN7oILO_g48JOmmMAadxUIg";
                MultipartFormDataContent form = new MultipartFormDataContent(boundary);
                var part1 = new StringContent(inputString, Encoding.GetEncoding("UTF-8"), "application/json");
                part1.Headers.Clear();
                part1.Headers.Add("Content-Disposition", "form-data; name=\"Input\"");
                part1.Headers.Add("Content-Type", "application/json; charset=UTF-8");
                form.Add(part1, "\"Input\"");
                var part2 = new StringContent(paramString, Encoding.GetEncoding("UTF-8"), "application/json");
                part2.Headers.Clear();
                part2.Headers.Add("Content-Disposition", "form-data; name=\"Parameters\"");
                part2.Headers.Add("Content-Type", "application/json; charset=UTF-8");
                form.Add(part2, "\"Parameters\"");
                form.Headers.Remove("Content-Type");
                form.Headers.TryAddWithoutValidation("Content-Type", "multipart/form-data; charset=ISO-8859-1; boundary=" + boundary);
                using HttpResponseMessage response = await client.PostAsync(url, form);
                response.EnsureSuccessStatusCode();
                var stream = await response.Content.ReadAsStreamAsync();
                //var parser = new StreamingMultipartFormDataParser(stream, boundary = "Nuance_VoCS_0");
                var parser = new StreamingMultipartFormDataParser(stream);
                parser.ParameterHandler += para =>
                {

                    Console.WriteLine(para.Name);
                    Console.WriteLine(para.Data);

                };
                parser.FileHandler += (name, fileName, type, disposition, buffer, bytes, partNumber, additionalProperties) =>
                {

                    Console.WriteLine($"audio bytes {bytes}");
                };

                parser.Run();


                await Console.Out.WriteLineAsync("=======================");


            }
            catch (HttpRequestException e)
            {
                Console.WriteLine(e.Message);
            }
            Console.WriteLine("end of main");
        }
    }
}